#!/bin/bash
#
# 时间: 2023-1-4
# 版本: 0.2
# 作者: li
#
# 脚本名称： 
# 脚本灵感来源: 
# 脚本可以完成的任务：展示服务基本信息，筛选出与重启相关的日志信息
# 脚本使用： ./collection_host_reboot_info.sh\

function collect_messages_log(){
  grep -B 10 "rsyslogd" /var/log/messages | grep "$(uptime -s | awk '{print $NF}' | cut -d : -f 1-2)" 
  if [ $? -eq 0 ];then
    state=1
  else
    ls /var/log/messages-"$(uptime -s | xargs -I{} date -d "{}" +'%Y%m%d')" > /dev/null  2>&1
    if [ $? -eq 0 ];then
      grep -B 10 "rsyslogd" /var/log/messages-"$(uptime -s | xargs -I{} date -d "{}" +'%Y%m%d')" | grep "$(uptime -s | awk '{print $NF}' | cut -d : -f 1-2)"
      state=1
    else
      state=0
    fi
  fi

  if [ $state -eq 0 ];then
    echo -e "\e[92m No messages log \e[0m"
  fi
}

function dmesg_log(){
  dmesg -T | grep "$(uptime -s | xargs -I{} date -d "{}" +'%b  %-d %H:%M')" | grep -Ei 'error|Fail'
  if [ $? -ne 0 ];then
    dmesg -T | grep "$(uptime -s | xargs -I{} date -d "{}" +'%b %-d %H:%M')" | grep -Ei 'error|Fail'
    if [ $? -ne 0 ];then
      echo -e "\e[92m No dmesg log \e[0m"
    fi
  fi
}

function crash_log(){
  systemctl status kdump > /dev/null
  if [ $? -eq 0 ];then
    if [ `ls /var/crash/ | wc -l` -gt 0 ];then
      whereis crash > /dev/null
      if [ $? -ne 0 ];then
        echo -e "\e[34m install crash \n\e[0m"
        yum install crash -y
      fi
      rpm -qa | grep kernel-debuginfo > /dev/null
      if [ $? -ne 0 ];then
        if ping baidu.com -c 1 >/dev/null 2>&1; then 
          curl -o /tmp/kernel-debuginfo-3.10.0-1160.31.1.el7.x86_64.rpm  http://debuginfo.centos.org/7/x86_64/kernel-debuginfo-3.10.0-1160.31.1.el7.x86_64.rpm
          curl -o /tmp/kernel-debuginfo-common-x86_64-3.10.0-1160.31.1.el7.x86_64.rpm http://debuginfo.centos.org/7/x86_64/kernel-debuginfo-common-x86_64-3.10.0-1160.31.1.el7.x86_64.rpm
          cd /tmp
          rpm -ivh kernel-debuginfo-3.10.0-1160.31.1.el7.x86_64.rpm 
          rpm -ivh kernel-debuginfo-common-x86_64-3.10.0-1160.31.1.el7.x86_64.rpm
          rm -f /tmp/{kernel-debuginfo-3.10.0-1160.31.1.el7.x86_64.rpm,kernel-debuginfo-common-x86_64-3.10.0-1160.31.1.el7.x86_64.rpm}
        else 
          echo -e "you need install \n kernel-debuginfo-$(uname -a | awk '{print $3}').rpm\n kernel-debuginfo-common-x86_64-$(uname -a | awk '{print $3}').rpm \n from http://debuginfo.centos.org/7/x86_64/"
        fi
      fi
      crash_log=($(find /var/crash/ -type f))
      for i in "${crash_log[@]}"; do
        crash $i
      done
    else
        echo -e "\e[92m No crash log \e[0m"
    fi
  else
    echo "kdump not start no crash log"
  fi
}

messages=$(collect_messages_log)
history_command=$(command_history)
dmesg=$(dmesg_log)
crash=$(crash_log)

echo -e "\e[36m-----------------------Collection Host reboot info script---------------------\e[0m"
echo -e "\e[34mlast reboot time:\t\e[0m`last -F reboot | head -1 | awk '{$1="";$2="";$3="";$4="";print $0}' | sed 's/^[ ]*//g'`"
echo -e "\e[34mSystem start time:\t\e[0m`uptime -s `"
echo -e "\e[34mRelevant message log:\t\e[0m\n${messages}"
echo -e "\e[34mdmesg error log:\t\e[0m\n${dmesg}"
echo -e "\e[34mcrash log:\t\t\e[0m\n${crash}"

